package cc.ufinity.networkdiagnose.widget;

import android.app.Activity;
import android.net.ConnectivityManager;
import android.os.Handler;
import android.os.Message;
import cc.ufinity.networkdiagnose.app.GlobalApp;
import cc.ufinity.networkdiagnose.manager.MessageWhat;
import cc.ufinity.networkdiagnose.manager.Task;
import cc.ufinity.networkdiagnose.manager.TelManager;
import cc.ufinity.networkdiagnose.view.R;

public class RssiTestWidgetProxy extends GeneralTestWidget {

    private Activity ctx;
    private Handler handlerChild;
    private final Handler handler;  
    private Task task;
	public static int testFlag = 0; 
    public static int taskFlag = 0;  
	private TelManager tm;
	
	private boolean TEL_RSSI_SUCCESS = false;
	private boolean isFirst = true;
	
    public RssiTestWidgetProxy(Activity context, Handler handl) {
        super(context);
        this.ctx = context;
        this.handlerChild = handl;
        inflate(context, R.layout.webauth_test_widget_proxy, this); 
        task = (Task)ctx.getIntent().getSerializableExtra(TAG_REFER_TASK);  
      
        handler = new Handler() {
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case MessageWhat.DO_NEXT_TEST:
                    //showNextText();
                    break; 
                 
                case MessageWhat.TEL_STATE_SUCCESS:
                	if(!TEL_RSSI_SUCCESS) {
                		TEL_RSSI_SUCCESS = true;
                		updateTel();
                	}
					break;
                }
            }
        };   
     
		tm = new TelManager(ctx);
		tm.setHandler(handler);
//		updateTel();
    } 
    
    private void updateTel() {
    	
    	int lac=0;
    	int ci=0;
    	if(tm!=null){
    		if(tm.getNetworkType().equals("HSDPA")||tm.getNetworkType().equals("HSUPA")){
            	GlobalApp.tdRrri=tm.getStrSignalStrength();
            	GlobalApp.tdCi=tm.getCI();
            	GlobalApp.tdLAc=tm.getLac();
            	lac = GlobalApp.tdLAc;
            	ci = GlobalApp.tdCi;
            	System.err.println(GlobalApp.tdRrri+"------------------GlobalApp.tdLAc----------------"+"\n\n\n"+GlobalApp.tdCi
            			+"------------------GlobalApp.tdLAc--------------"+"\n\n\n"+GlobalApp.tdLAc);
        	}else {
        		GlobalApp.gsmTssi=tm.getStrSignalStrength();	
        		GlobalApp.GsmCi=tm.getCI();
            	GlobalApp.GsmLAc=tm.getLac();
            	lac = GlobalApp.GsmLAc;
            	ci = GlobalApp.GsmCi;
            	System.err.println(GlobalApp.gsmTssi+"------------------GlobalApp.gsmLAc----------------"+"\n\n\n"+GlobalApp.GsmCi
            			+"------------------GlobalApp.gsmLAc--------------"+"\n\n\n"+        	GlobalApp.GsmLAc);
        	}
    	}
    	
    	
    	System.out.println(task.selNetType+"================="+tm.getSignalStrength()+"=============="+task.rssi);
    	if(task.selNetType == ConnectivityManager.TYPE_MOBILE || task.selNetType == ConnectivityManager.TYPE_MOBILE_MMS)
        {
        	  task.rssi = String.valueOf(tm.getSignalStrength());
        	  System.out.println("TD网络 信号强度： -" + tm.getSignalStrength());
              //----------------------------------------------------------
        		if(isFirst) {
        			isFirst = false;
        			StringBuffer temp = new StringBuffer();
        			GlobalApp.result_SRRI_test = temp;
        		}
        		GlobalApp.result_SRRI_test.append("RSSI: " + task.rssi).
        								   append("\nLAC: "+lac).
        								   append("\nCI："+ci);
        		
        	//----------------------------------------------------------
        }else { 
        	  System.out.println(task.selNetType+"==============================="+task.scanResult.level);
              task.rssi =String.valueOf( task.scanResult.level);
            //----------------------------------------------------------
      		if(isFirst) {
      			isFirst = false;
      			StringBuffer temp = new StringBuffer();
      			GlobalApp.result_SRRI_test = temp;
      		}
      		GlobalApp.result_SRRI_test.append("SSID: " + task.scanResult.SSID)
      				.append("\nMAC: " + task.scanResult.BSSID)
      				.append("\nRSSI: " + task.rssi);
      	//----------------------------------------------------------
		}
      

System.out.println("task.rssi => " + task.rssi);
    	
        //mSendBroadcast(ctx);
		handlerSendToTarget(handlerChild, MessageWhat.HANDLER_RSSI);
        
        ctx.getIntent().putExtra(TAG_REFER_TASK, task);
    }
 
}
